package br.com.sig.security.presentation.action;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.seam.international.status.builder.BundleKey;

import br.com.sig.business.exception.BaseException;
import br.com.sig.presentation.action.BaseAction;
import br.com.sig.presentation.action.Operation;
import br.com.sig.security.BaseIdentity;
import br.com.sig.security.Identity;
import br.com.sig.security.business.model.bo.UserBO;
import br.com.sig.security.business.model.entity.User;

@Named
@ViewScoped
public class ChangePasswordAction extends BaseAction<User> {

	private static final long serialVersionUID = -1634914936667632291L;
	
	private String currentPassword;
	private String newPassword;

	@Inject
	private UserBO userBO;
	
	@Inject
	@Identity
	private BaseIdentity identity;

	@PostConstruct
	public void init() {
		setService(userBO);
		setOperation(Operation.EDIT);
	}
	
	public void changePassword() {
		try {
			userBO.changePassword(this.identity.getLoggedUser(), this.currentPassword, this.newPassword);
			messages.info(new BundleKey("messages", "info.change.password"));
		} catch (BaseException e) {
			messages.error(new BundleKey("messages", e.getMessage()));
		}
	}

	public String getCurrentPassword() {
		return currentPassword;
	}

	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
}